package vehicle

import (
	"fmt"
	log2 "github.com/name5566/leaf/log"
	"server/cfg"
	"server/game/bag"
	"server/game/item"
	"strconv"
	"strings"
)

func NewVehicle(userId string, vtId int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	info, err := logic.NewVehicle(userId, vtId)
	if err != nil {
		return ret, err
	}
	ret["info"] = info
	return ret, nil
}

func VehicleInfo(userId string, vtId int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	info, err := logic.GetVehicleInfo(userId, vtId)
	if err != nil {
		return ret, err
	}
	ret["info"] = info
	return ret, nil
}
func VehicleList(userId string) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	info, err := logic.GetVehicleList(userId)
	if err != nil {
		return ret, err
	}
	ret["list"] = info
	return ret, nil
}
func UpdateVehicleLevel(userId string, vtId, step int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	config := cfg.ConfigData.TbCar.Get(int32(vtId))
	_, err := bag.EnUseItem(userId, item.CarUpLevelCostItemId, int(config.UpgradeConsumeCount))
	if err != nil {
		log2.Error("升级失败，材料不够")
		return nil, fmt.Errorf("升级失败，材料不够")
	}
	logic := NewVehicleLogic()
	list, err := logic.UpdateLevel(userId, vtId, step)
	if err != nil {
		return ret, err
	}
	ret["list"] = list
	return ret, nil

}
func DestroyCar(userId string, vtId int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	config := cfg.ConfigData.TbCar.Get(int32(vtId))
	logic := NewVehicleLogic()
	err := logic.DestroyCar(userId, vtId)
	if err != nil {
		return ret, err
	}
	itemArray := strings.Split(config.RecycleReward, "_")
	itemId, _ := strconv.Atoi(itemArray[0])
	itemNum, _ := strconv.Atoi(itemArray[1])
	bagList, err := bag.EnAddItem(userId, itemId, itemNum)
	if err != nil {
		log2.Error("分解材料失败，用户id %s,材料id%d,数量%d", userId, itemId, itemNum)
		//return nil, fmt.Errorf("升级失败，材料不够")
	}
	ret["bag_list"] = bagList
	return ret, nil

}
func UnLockVehicleSkillInfo(userId string, vtId int64, skillId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	list, err := logic.UnLockSkillInfo(userId, vtId, skillId)
	if err != nil {
		return ret, err
	}
	ret["list"] = list
	return ret, nil
}
func VehicleSkillRandNum(userId string, vtId int64, skillId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	list, err := logic.RandNum(userId, vtId, skillId)
	if err != nil {
		return ret, err
	}
	ret["list"] = list
	return ret, nil
}

func UpVehicleSkillLevel(userId string, vtId int64, skillId int32) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	logic := NewVehicleLogic()
	list, err := logic.UpdateSkillLevel(userId, vtId, skillId)
	if err != nil {
		return ret, err
	}
	ret["list"] = list
	return ret, nil
}
